package com.taysear.bus;

import java.util.HashMap;

import com.taysear.bus.util.Results;
import com.taysear.bus.util.Util;
import com.taysear.dbservice.DbServicesPrx;
import com.taysear.dbservice.ServiceParameter;
import com.taysear.dbservice.ServiceResult;
import com.taysear.domain.Employee;
import com.taysear.domain.Permission;
import com.taysear.domain.User;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.MenuBar.MenuItem;
import com.vaadin.ui.CustomLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;

/**
 * <code>MainLayout</code> is used to create the main layout of the application.
 * 
 * @author Abdullahi
 */
@SuppressWarnings("serial")
public class MainLayout extends VerticalLayout {

	private HorizontalLayout topBar = new HorizontalLayout();
	private HorizontalLayout middleBar = new HorizontalLayout();
	private final HorizontalLayout center = new HorizontalLayout();
	private CustomLayout customLayout = new CustomLayout("mainLayout");
	
	/**
	 * Creates a layout with on the top the logo, username and logout button. 
	 * In the middle the work area where all the application form and windows 
	 * will be displayed and at the bottom the a footer.
	 * 
	 */
	public MainLayout() {
		customLayout.addStyleName("mainLayout");
		topBar.setSpacing(true);
		
		center.setSizeUndefined();
		
		customLayout.addComponent(middleBar, "menubar");	
		customLayout.addComponent(center, "content");
		
		center.addStyleName("verticalAlignCenter");
		center.addComponent(new LoginScreen());
        
		addComponent(customLayout);
		setSizeFull();
	}
	
	/**
	 * Loads the menu of the application, displays the user name, 
	 * display a button that can be used to logout of the application.
	 */
	public void loadMenuAndUserInfo() {
		center.removeAllComponents();
		center.removeStyleName("verticalAlignCenter");
		
		Label welcomeLabel = new Label("Welcome, "+ AppData.getUser());
		topBar.addComponent(welcomeLabel);
		
		Button logout = new Button("Logout");
		logout.addStyleName("redButton");
		logout.setDisableOnClick(true);
		logout.addListener(new Button.ClickListener() {
		    @Override
		    public void buttonClick(ClickEvent event) {
		    	getApplication().close();
		    } 
		});
		topBar.addComponent(logout);
		customLayout.addComponent(topBar, "userInfoLogoutBtn");
		
		customLayout.addComponent(getPortalMenus(),"portalMenu");
	}
	
	/**
	 * Gets the portals menus.
	 * 
	 * @return MenuBar
	 */
	private MenuBar getPortalMenus() {
		BusApplication app = AppData.getApp();
		MenuBar menuBar = null;
		
		ServiceParameter sp = new ServiceParameter(new HashMap<String, String>());
		sp.param.put("userId", AppData.getUser().getUserId().toString());
		sp.param.put("portalId", AppData.getPortalId());
		
		DbServicesPrx dbServicesPrx = app.getDbServiceProxy();
		ServiceResult sr = dbServicesPrx.getPermissionsByUserIdAndPortalId(sp);
		if(sr.status) {
			if(sr.param.get("RECORDS") != null) {
				Results r = Util.getObjectsFromXmlResult(sr.param.get("RECORDS"), Permission.class);
				menuBar = new MenuBar(r.getRows());
			}
		} else {
			app.showErrorNotification("Faild to load menus");
		}
		
		return menuBar;
	}

	/**
     * Notifies all contained components that the container is attached to a
     * window.
     * 
     * @see com.vaadin.ui.Component#attach()
     */
    @Override
    public void attach() {
        super.attach();
        
        Label portalName = new Label(getApplication().getProperty("portalName"));
		customLayout.addComponent(portalName, "portalName");
		
		Label version = new Label(getApplication().getProperty("version"));
		customLayout.addComponent(version, "version");
    }

	public HorizontalLayout getCenter() {
		return center;
	}
}